package org.yzz.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yzz.auth.annotation.RonghuaPermission;
import org.yzz.base.util.AjaxResult;
import org.yzz.base.util.PageInfo;
import org.yzz.org.domain.Department;
import org.yzz.org.query.DepartmentQuery;
import org.yzz.org.service.IDepartmentService;
import java.util.List;
import java.util.Objects;

@RestController
@RonghuaPermission(name = "部门管理")
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping
    @RonghuaPermission(name = "查询全部部门")
    @ApiOperation(value = "查询全部部门" )
    public AjaxResult selectAll(){
        try {
            List<Department> Departments = departmentService.selectAll();
            return AjaxResult.success(Departments);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    @GetMapping("/{id}")
    @RonghuaPermission(name = "通过ID查询部门")
    @ApiOperation(value = "通过ID查询部门" )
    public AjaxResult selectById(@PathVariable Long id){
        try {
            Department Department = departmentService.selectById(id);
            return AjaxResult.success(Department);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    //通过名字查询
    /*@GetMapping("/{username}")
    public void selectByName(String username){

    }*/

    /**
     * 新增或修改
     * @param Department
     */
    @PostMapping
    @RonghuaPermission(name = "部门新增或修改")
    @ApiOperation(value = "新增或修改" )
    public AjaxResult save(@RequestBody Department Department){
        try {
            if(Objects.nonNull(Department.getId())){
                departmentService.update(Department);
                return AjaxResult.success();
            }
            departmentService.insert(Department);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @RonghuaPermission(name = "部门根据指定 id 删除")
    @ApiOperation(value = "删除" )
    public AjaxResult delete(@PathVariable Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param departmentQuery
     * @return
     */
    @PutMapping
    @RonghuaPermission(name = "部门分页查询")
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery){
        try {
            PageInfo<Department> pageInfo = departmentService.queryPage(departmentQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PatchMapping
    @RonghuaPermission(name = "部门批量删除")
    public  AjaxResult batchDelete(@RequestBody  List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/deptTree")
    @RonghuaPermission(name = "查询部门树")
    public AjaxResult deptTree(){
        try {
            List<Department> list = departmentService.deptTree();
            return AjaxResult.success(list);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
